package com.eastfair.venueservice.entity;

import cn.hutool.core.map.MapUtil;
import com.eastfair.boot.mapper.SuperMapper;
import com.eastfair.boot.service.SuperService;
import com.eastfair.constant.BusinessConstant;
import lombok.Data;
import org.apache.poi.ss.formula.functions.T;
import com.baomidou.mybatisplus.core.conditions.Wrapper;

import java.util.*;

/**
 * 随机编码生成
 * @author linan
 */
@Data
public class CreatCode {

    /**
     *
     * @param superMapper mapper对象
     * @param column 数据库字段名
     * @param len 随机数长度
     * @param prefix 前缀
     * @return
     */
    public static String creatCode(SuperMapper<?> superMapper,String column,Integer len,String prefix) {
        len = Objects.nonNull(len) ?len:5;
        boolean b = true;
        String finalCode = "";
        while (b) {
            String code = getRandom(len);
            finalCode = String.format("%s%s", prefix, code);
            List<?> list = superMapper.selectByMap(MapUtil.<String, Object>builder().put(column, finalCode).put("is_deleted", BusinessConstant.NO).build());
            if (list.size() == 0) {
                b = false;
            }
        }
        return finalCode;
    }

    public static String getRandom(int len){
        long rs =(long)((Math.random()*9+1)*Math.pow(10,len-1));
        return String.valueOf(rs);
    }
}
